from time import sleep
from ddt import ddt, file_data
from appium import webdriver
from common.pyyaml import getyaml
from common.parame import Parmer
from selenium.common.exceptions import NoSuchElementException
from config import apk_path, login_element
from common.isintsallapp import isinstallapp

element = getyaml(login_element)

@ddt
class testCase(Parmer):
    def setUp(self) -> None:
        is_first_install = isinstallapp(devicesName=self.parme['deviceName'], apk_path=apk_path,
                                        apppackage=self.parme['appPackage'])
        desired_caps = self.parme['desired_caps']
        self.driver = webdriver.Remote('http://0.0.0.0:%s/wd/hub' % str(self.parme['port']), desired_caps)
        sleep(10)
        # 启动同意用户协议
        agree = element['agreeelement']
        self.driver.find_element_by_id(agree).click()
        if is_first_install:
            # 首次安装需要加载文件
            sleep(50)
        sleep(11)
        self.driver.find_element_by_id(element['opearelement']).click()
        sleep(5)
        try:
            self.driver.find_element_by_xpath(element['logintextclik']).click()
        except NoSuchElementException as e:
            print(e)

    def tearDown(self) -> None:
        self.driver.quit()
        sleep(20)

    @file_data("../data/casedata/logincase.yaml")
    def testlogin(self, user, user_password, msg):
        sleep(10)
        username = self.driver.find_element_by_id(element['usernameedit'])
        username.clear()
        username.send_keys(user)
        password = self.driver.find_element_by_id(element['passwordedit'])
        password.clear()
        password.send_keys(user_password)
        login = self.driver.find_element_by_id(element['loginbtn'])
        login.click()
        try:
            self.driver.find_element_by_id(element['assertelement'])
            self.assertTrue(False, msg=msg)
        except:
            self.assertTrue(True, msg=msg)
